﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using libDAL;
using libInventory.BUS ;
using System.Data;
using System.Windows.Forms;

namespace libInventory.BUS
{
    sealed class clsBUSFindCharges : IDisposable 
    {
        private  cls_DAL _DALSalesTerm;
        private DataTable dtCharges;
        private string strSearchType = string.Empty;
        private string strSalesTermSetupHeaderID = string.Empty;
        private DataTable dtAdditionalServices;
        public string getset_SalesTermSetupHeaderID
        {
            get
            {
                return strSalesTermSetupHeaderID;
            }
            set
            {
                strSalesTermSetupHeaderID = value;
            }
        }
        private string get_QueryCharges()
        {

            return "select ccitemcode as [Item Code(s)], ccitemdes as [Description(s)] " +
                   "from mfchaitemmain " +
                   "group by ccitemcode, ccitemdes " +
                   "order by ccitemcode asc";
        }

        private string get_QueryChargesBySetupHeader(string strSetupHeaderID)
        {
            return "SELECT SalesTermSetupAdditionalCharges.SalesTermSetupAdditionalChargesID, SalesTermSetupAdditionalCharges.SalesTermSetupHeadersID, " +
                   "SalesTermSetupAdditionalCharges.ccitemcode as [Item Code(s)], mfchaitemmain.ccitemdes as [Description(s)], ChargeType AS Type " +
                   "FROM   SalesTermSetupAdditionalCharges LEFT OUTER JOIN " +
                   "mfchaitemmain ON SalesTermSetupAdditionalCharges.ccitemcode = mfchaitemmain.ccitemcode " +
                   "where SalesTermSetupAdditionalCharges.SalesTermSetupHeadersID = '" + strSetupHeaderID + "' " +
                   "order by ChargeType asc ";
        }

        private string get_QueryChargesAll(string strSetupHeaderID)
        {
            return "SELECT SalesTermSetupAdditionalChargesID, SalesTermSetupHeadersID, RateM3, MinRateM3, MinAmountM3, RateWeight, MinRateWeight, MinAmountWeight, RateVolume, " +
                   "MinRateVolume, MinAmountVolume, RateQuantity, MinRateQuantity, MinAmountQuantity, InvoicePercentage, ChargeType, DateTimeInserted, DateTimeUpdated, " +
                   "LastModifiedBy, HostNameIPAddress, OnUse, OnUseBy, ccitemcode, ExpiryFrom, ExpiryTo, PerShipment, NoRating " +
                   "FROM SalesTermSetupAdditionalCharges " +
                   "WHERE SalesTermSetupHeadersID = '" + strSetupHeaderID + "' " +
                   "ORDER BY ExpiryFrom desc, ExpiryTo desc";
        }

        public clsBUSFindCharges()
        {
           // _BUS = new clsBUSFindCharges();
            dtAdditionalServices = new DataTable();
        }

        public cls_DAL _DAL
        {
            set
            {
                _DALSalesTerm = value;
            }
        }

        //dtAdditionalServices

        public DataTable get_SearchResult(string strDescription, bool bolStatus)
        {

            DataTable dt = new DataTable();
            try
            {
                DataRow[] dr = { };
                if (bolStatus == true)
                {
                    dr = dtCharges.Select("[Item Code(s)] like '*" + strDescription + "*'", "[Item Code(s)] asc");
                }
                else
                {
                    dr = dtCharges.Select("[Description(s)] LIKE '*" + strDescription + "*'", "[Description(s)] asc");
                }
                dt = dr.CopyToDataTable();
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.InnerException.Message + " get_SearchResult"); 
            }
            return dt;
        }

        public DataTable get_ChargesSetup(string strSetupDetailsID)
        {
            DataTable dtCharges = new DataTable();
            try
            {

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + "get_ChargesSetup"); 
            }
            return dtCharges;
        }
        
        public DataTable get_Charges()
        {
            try
            {
                if (getset_SalesTermSetupHeaderID == string.Empty)
                {
                    dtCharges = new DataTable();
                    dtCharges = _DALSalesTerm.get_TableDetail(get_QueryCharges());
                }
                else if(getset_SalesTermSetupHeaderID != string.Empty)
                {
                    dtCharges = new DataTable();
                    dtAdditionalServices = new DataTable();
                    this.dtCharges = _DALSalesTerm.get_TableDetail(get_QueryChargesBySetupHeader(getset_SalesTermSetupHeaderID));
                    dtAdditionalServices = _DALSalesTerm.get_TableDetail(get_QueryChargesAll(getset_SalesTermSetupHeaderID));
                }
                //dtCharges
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.InnerException.Message + " get_Charges");
            }
            return dtCharges;
        }

        public void Dispose()
        {
            GC.Collect();
        }

        public DataTable get_DetailsSetupDetails(string strDetailsID)
        {
            DataTable dtDetails = new DataTable();
            try
            {
                if (dtAdditionalServices != null)
                {
                    if (dtAdditionalServices.Rows.Count > 0)
                    {
                        DataRow[] drCheck = dtAdditionalServices.Select("SalesTermSetupAdditionalChargesID = '" + strDetailsID + "'");
                        if (drCheck.Length > 0)
                        {
                            dtDetails = drCheck.CopyToDataTable();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + " get_DetailsSetupDetails");
            }
            return dtDetails;
        }
    }
}
